public class DynamicProgram2 {

//    打家劫舍2  环形

    public int rob(int[] nums) {
        if (nums == null) {
            return -1;
        }
        int length = nums.length;
        if (length == 1) {
            return nums[0];
        }
        if (length == 2) {
            return Math.max(nums[0],nums[1]);
        }
        return Math.max(robRange(nums,0,length-2),robRange(nums,1,length-1));
    }
    public int robRange(int[] nums, int start, int end) {
        int first = nums[start];
        int second = Math.max(nums[start],nums[start+1]);
        for (int i = start+2; i <= end; i++) {
            int tmp = second;
            second = Math.max(second,first+nums[i]);
            first = tmp;
        }
        return second;
    }
}
